  
 namespace :blast do
 
   desc "Fire off all new blasts"
   task :off => :environment do
    p "running blast:off"

    blasts = Blast.find(:all, :conditions=>["sent_at = created_at",nil])
    for b in blasts
      b.sent_at = Time.now()
      BlastMailer.deliver_blast(b)
      # update attributes here so we know the email was sent...
      b.save()
      p "#{b.from} just blasted #{b.to}..."
    end
   end

   desc "Replace acts_as_blast template files from specified directories"
   task :replace_templates do
    require "ftools"
    p "running blast:replace_templates"

    app_views = "app/views"
    app_controllers = "app/controllers"
    app_models = "app/models"
    app_helpers = "app/helpers" 
    public_js = "public/javascripts"
    public_css = "public/stylesheets" 
    test_unit = "test/unit"     
    test_fixtures = "test/fixtures" 
    config = "config" 
    generator_root = "vendor/plugins/acts_as_blast/lib/generators/blast/templates"
    
    files ={
      ### controllers ###
      "#{app_controllers}/blasts_controller.rb" => "#{generator_root}/#{app_controllers}/blasts_controller.rb",
      
      ### models ###
      "#{app_models}/blast.rb" => "#{generator_root}/#{app_models}/blast.rb",
      "#{app_models}/blast_mailer.rb" => "#{generator_root}/#{app_models}/blast_mailer.rb",
      
      ### helpers ###
      "#{app_helpers}/blasts_helper.rb" => "#{generator_root}/#{app_helpers}/blasts_helper.rb",
    
      ### views ###
      "#{app_views}/blast_mailer/blast.rhtml" => "#{generator_root}/#{app_views}/blast_mailer/blast.rhtml",
      "#{app_views}/blast_mailer/_user.rhtml" => "#{generator_root}/#{app_views}/blast_mailer/_user.rhtml",

      "#{app_views}/blasts/_form.rhtml" => "#{generator_root}/#{app_views}/blasts/_form.rhtml", 
      "#{app_views}/blasts/_blast.rhtml" => "#{generator_root}/#{app_views}/blasts/_blast.rhtml",
      "#{app_views}/blasts/new.html.erb" => "#{generator_root}/#{app_views}/blasts/new.html.erb",
      "#{app_views}/blasts/example.html.erb" => "#{generator_root}/#{app_views}/blasts/example.html.erb",      
      "#{app_views}/blasts/back.html.erb" => "#{generator_root}/#{app_views}/blasts/back.html.erb",            
      "#{app_views}/layouts/blasts.html.erb" => "#{generator_root}/#{app_views}/layouts/blasts.html.erb",
      
      ### javascripts ### 
      "#{public_js}/blast.js" => "#{generator_root}/#{public_js}/blast.js",   
      
      ### stylesheets ### 
      "#{public_css}/blast.css" => "#{generator_root}/#{public_css}/blast.css", 
 
       ### test/unit ### 
      "#{test_unit}/blast_mailer_test.rb" => "#{generator_root}/#{test_unit}/blast_mailer_test.rb",   
      "#{test_unit}/blast_test.rb" => "#{generator_root}/#{test_unit}/blast_test.rb",   
      
       ### test/fixtures ### 
      "#{test_fixtures}/mailer/mailer" => "#{generator_root}/#{test_fixtures}/mailer/mailer",   
      "#{test_fixtures}/blasts.yml" => "#{generator_root}/#{test_fixtures}/blasts.yml",
      "#{test_fixtures}/users.yml" => "#{generator_root}/#{test_fixtures}/users.yml",
      
      ### config ### 
      "#{config}/blasts.yml" => "#{generator_root}/#{config}/blasts.yml"      
    
    }    
    
    # make new directories
    p "creating file directories..."

    ### controllers
    FileUtils.mkdir_p "#{generator_root}/#{app_controllers}"
    ### helpers
    FileUtils.mkdir_p "#{generator_root}/#{app_helpers}"
    ### models
    FileUtils.mkdir_p "#{generator_root}/#{app_models}"
    ### views
    FileUtils.mkdir_p "#{generator_root}/#{app_views}/blasts"    
    FileUtils.mkdir_p "#{generator_root}/#{app_views}/blast_mailer"
    FileUtils.mkdir_p "#{generator_root}/#{app_views}/layouts"
    ### public/javascripts
    FileUtils.mkdir_p "#{generator_root}/#{public_js}"    
    ### public/css
    FileUtils.mkdir_p "#{generator_root}/#{public_css}"   
    ### test/unit
    FileUtils.mkdir_p "#{generator_root}/#{test_unit}"      
    ### test/fixtures
    FileUtils.mkdir_p "#{generator_root}/#{test_fixtures}"      
    ### config
    FileUtils.mkdir_p "#{generator_root}/#{config}"
     
    files.to_a.each{ | file | 
      p "copying: #{file[0]} => #{file[1]}" 
      File.copy(file[0], file[1])
    }
    
    

   end
 
   desc "copy files to testing project"
   task :build_test_app do

    require "ftools"
    p "running blast:build_test_app"
    testing_root = "../test_acts_as_blast"
    files = {  
      "app/models/user.rb" => "#{testing_root}/app/models/user.rb",
      "config/database.yml" => "#{testing_root}/config/database.yml",
      "config/environment.rb" => "#{testing_root}/config/environment.rb",      
      "config/routes.rb" => "#{testing_root}/config/routes.rb", 
      "lib/smtp_tls.rb" => "#{testing_root}/lib/smtp_tls.rb", 
    }
    
    # cleanup: previous test app
    system("rm -rf ../test_acts_as_blast")
    # make new test app
    system("rails test_acts_as_blast")
    # move to ../ 
    system("mv -f test_acts_as_blast ../")
    system("ls", "-l")

    FileUtils.mkdir_p "#{testing_root}/lib/tasks"

    # copy new files to test app
    files.to_a.each{ | file | 
      p "copying: #{file[0]} => #{file[1]}" 
      File.copy(file[0], file[1])
    }    

   
   end
 
 end